package com.jingdianjichi.interview.server.engine;

import com.jingdianjichi.interview.api.enums.EngineEnum;
import com.jingdianjichi.interview.api.req.InterviewSubmitReq;
import com.jingdianjichi.interview.api.req.StartReq;
import com.jingdianjichi.interview.api.vo.InterviewQuestionVO;
import com.jingdianjichi.interview.api.vo.InterviewResultVO;
import com.jingdianjichi.interview.api.vo.InterviewVO;
import com.jingdianjichi.interview.server.entity.po.SubjectCategory;
import com.jingdianjichi.interview.server.entity.po.SubjectInfo;
import com.jingdianjichi.interview.server.entity.po.SubjectLabel;
import com.jingdianjichi.interview.server.mapper.SubjectDao;
import com.jingdianjichi.interview.server.utils.EvaluateUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 鸡翅引擎
 */
@Service
public class JiChiInterviewEngine implements InterviewEngine {

    //标签
    private List<SubjectLabel> labels;

    //分类
    private Map<Long, SubjectCategory> categoryMap;


    //初始化,你也可以使用生命周期实现
    @PostConstruct
    public void init() {
        //查询所有标签
        labels = subjectDao.listAllLabel();

        //这段Java代码使用Stream API将SubjectCategory对象列表转换为一个Map，其键为对象的id，值为对象本身。具体步
        categoryMap = subjectDao.listAllCategory().stream().collect(Collectors.toMap(SubjectCategory::getId, Function.identity()));
    }

    @Resource
    private SubjectDao subjectDao;


    //引擎的类型
    @Override
    public EngineEnum engineType() {
        return EngineEnum.JI_CHI;
    }

    //简历分析
    @Override
    public InterviewVO analyse(List<String> KeyWords) {
        if (CollectionUtils.isEmpty(KeyWords)) {
            return new InterviewVO();
        }

        List<InterviewVO.Interview> views = this.labels.stream()
                .filter(item -> KeyWords.contains(item.getLabelName()))
                .map(item -> {
                    //构建内部类返回
                    InterviewVO.Interview interview = new InterviewVO.Interview();

                    //查询标签对应的分类
                    SubjectCategory subjectCategory = categoryMap.get(item.getCategoryId());

                    //拼接名称返回
                    if (Objects.nonNull(subjectCategory))
                    {
                        interview.setKeyWord(String.format("%s-%s", subjectCategory.getCategoryName(), item.getLabelName()));
                    }
                    else
                    {
                        interview.setKeyWord(item.getLabelName());
                    }

                    interview.setCategoryId(item.getCategoryId());
                    interview.setLabelId(item.getId());

                    return interview;
                }).collect(Collectors.toList());

        InterviewVO vo = new InterviewVO();
        vo.setQuestionList(views);

        //返回
        return vo;
    }

    //获得模拟面试题
    @Override
    public InterviewQuestionVO start(StartReq req) {
        //获得所有的标签id
        List<Long> ids = req.getQuestionList().stream().map(StartReq.Key::getLabelId).distinct().collect(Collectors.toList());

        if (CollectionUtils.isEmpty(ids)) {
            return new InterviewQuestionVO();
        }

        //查询所有标签
        List<SubjectInfo> subjectInfos = subjectDao.listSubjectByLabelIds(ids);

        //构造返回值
        List<InterviewQuestionVO.Interview> views = subjectInfos.stream().map(item -> {
            InterviewQuestionVO.Interview view = new InterviewQuestionVO.Interview();
            view.setSubjectName(item.getSubjectName());
            view.setSubjectAnswer(item.getSubjectAnswer());
            view.setLabelName(item.getLabelName());
            view.setKeyWord(String.format("%s-%s", item.getCategoryName(), item.getLabelName()));

            return view;
        }).collect(Collectors.toList());

        //如果大于8个体,我们就随机打乱之后返回前8个题
        if (views.size() > 8) {
            Collections.shuffle(views);
            views = views.subList(0, 8);
        }

        //构造返回值
        InterviewQuestionVO vo = new InterviewQuestionVO();
        vo.setQuestionList(views);

        //返回
        return vo;
    }

    //提交面试题,算面试评价每个标签的评价和平均分
    @Override
    public InterviewResultVO submit(InterviewSubmitReq req) {
        List<InterviewSubmitReq.Submit> submits = req.getQuestionList();

        //总分
        double total = submits.stream().mapToDouble(InterviewSubmitReq.Submit::getUserScore).sum();

        //评价分
        double avg = total / submits.size();

        //评价
        String avtTips = EvaluateUtils.avgEvaluate(avg);

        //获得每个题的评价
        String tips = submits.stream().map(item -> {
            String keyWord = item.getLabelName();
            String evaluate = EvaluateUtils.evaluate(item.getUserScore());

            //拼接评价
            return String.format(evaluate, keyWord);
        }).distinct().collect(Collectors.joining(";"));     //去重亲,评价可能是会重复的

        //构造返回值
        InterviewResultVO vo = new InterviewResultVO();
        vo.setAvgScore(avg);
        vo.setTips(tips);
        vo.setAvgTips(avtTips);

        //返回
        return vo;

    }

}
